import {createApp} from './main'

export default context => {
  return new Promise((resolve,reject)=>{
    const {app,router,store} = createApp(context);
    //这个地方处理App页面中的asyncData函数，并取值
    router.push(context.url);
    router.onReady(()=>{
      //获取匹配路由数组
      const matchedComponents = router.getMatchedComponents();
      //若无异常则抛出异常
      if(!matchedComponents){
        return reject({code:404})
      }
      Promise.all(
        matchedComponents.map(Component=>{
          if(Component.asyncData){
            return Component.asyncData({
              store
            })
          }
        })
      ).then(()=>{
        context.state = store.state
        resolve(app)
      })
    },reject)
  })
}